package com.example.zp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private WechatFragment wechatFragment = new WechatFragment();
    private Fragment friendFragment = new FriendFragment();
    private Fragment addressFragment = new AddressFragment();
    private Fragment settingFragment = new SettingFragment();

    private FragmentManager fm;

    private LinearLayout wechatTab;
    private LinearLayout friendTab;
    private LinearLayout addressTab;
    private LinearLayout settingTab;

    private ImageButton wechatImg;
    private ImageButton friendImg;
    private ImageButton addressImg;
    private ImageButton settingImg;

    private RecyclerView recyclerView;
    private List<String> list;
    private Context context;
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEvent();
        // 初始化时位置在首页
        selectFragment(0);
    }

    /**
     * 初始化FragmentManager，并放入页面
     */
    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        // 将四个页面放入 fragmentLayout 中
        transaction.add(R.id.id_content, wechatFragment);
        transaction.add(R.id.id_content, friendFragment);
        transaction.add(R.id.id_content, addressFragment);
        transaction.add(R.id.id_content, settingFragment);
        transaction.commit();
    }

    /**
     * 初始化响应区
     */
    private void initView() {
        // 获取Layout和ImageButton
        wechatTab = findViewById(R.id.tab_wechat);
        friendTab = findViewById(R.id.tab_friend);
        addressTab = findViewById(R.id.tab_address);
        settingTab = findViewById(R.id.tab_setting);

        wechatImg = findViewById(R.id.img_wechat);
        friendImg = findViewById(R.id.img_friend);
        addressImg = findViewById(R.id.img_address);
        settingImg = findViewById(R.id.img_setting);
    }

    /**
     * 初始化点击事件，将监听过程限制在 四个LinearLayout
     */
    private void initEvent() {
        wechatTab.setOnClickListener(this);
        friendTab.setOnClickListener(this);
        addressTab.setOnClickListener(this);
        settingTab.setOnClickListener(this);
    }

    /**
     * 切换tab 的函数，包含隐藏和高亮
     * @param i
     */
    private void selectFragment(int i) {
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        // 高亮显示
        switch (i) {
            case 0:
                transaction.show(wechatFragment);
//                initWechat();
                wechatImg.setImageResource(R.drawable.wechat_press);
                break;
            case 1:
                transaction.show(friendFragment);
                friendImg.setImageResource(R.drawable.friend_press);
                break;
            case 2:
                transaction.show(addressFragment);
                addressImg.setImageResource(R.drawable.address_press);
                break;
            case 3:
                transaction.show(settingFragment);
                settingImg.setImageResource(R.drawable.setting_press);
            default:
                break;
        }
        transaction.commit();
    }

    /**
     * 将之前显示的 Fragment 隐藏
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(wechatFragment);
        transaction.hide(friendFragment);
        transaction.hide(addressFragment);
        transaction.hide(settingFragment);
    }

    /**
     * 复原下方图标
     */
    private void resetImgs() {
        wechatImg.setImageResource(R.drawable.wechat);
        friendImg.setImageResource(R.drawable.friend);
        addressImg.setImageResource(R.drawable.address);
        settingImg.setImageResource(R.drawable.setting);
    }

    @Override
    public void onClick(View v) {

        resetImgs();
        switch (v.getId()) {
            case R.id.tab_wechat:
                selectFragment(0);
                break;
            case R.id.tab_friend:
                selectFragment(1);
                break;
            case R.id.tab_address:
                selectFragment(2);
                break;
            case R.id.tab_setting:
                selectFragment(3);
                break;
            default:
                break;
        }
    }

    public void initWechat() {
//        // 给环境赋值
//        context = this;
//
//
//        recyclerView = getSupportFragmentManager().findFragmentById(R.id.tab_wechat).getView().findViewById(R.id.recyclerView);
//        list = new ArrayList<>();
//        for (int i = 0;i < 9;i++) {
//            list.add("这是第" + i + "个例子");
//        }
//
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//
//        adapter = new Adapter(context, list);
//
//        recyclerView.setLayoutManager(linearLayoutManager);
//        recyclerView.setAdapter(adapter);
//        wechatFragment.init();
    }
}
